package com.kcsoft.controller;

import com.kcsoft.pojo.KeyAll;
import com.kcsoft.pojo.KeyTotal;
import com.kcsoft.pojo.Result;
import com.kcsoft.service.KeyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/key")
public class KeyWordController {

    @Autowired
    private KeyWordService keyWordService;

    @PostMapping("/save")
    public Result save(@RequestBody Map<String, String> rowData) {
        String data = rowData.get("keyout");
        if (data.equals("not"))
            return Result.success();
        String[] keys = data.split("，");
        for (String key : keys) {
            keyWordService.saveKey(key);
        }
        return Result.success();
    }

    @PostMapping("/DayTotal")
    public Result<Map<String, Integer>> total(@RequestParam LocalDate date) {
        List<KeyTotal> keyTotals = keyWordService.getKeyTotals(date);
        Map<String, Integer> map = keyTotals.stream()
                .collect(Collectors.toMap(KeyTotal::getKeyWord, KeyTotal::getTotal));
        return Result.success(map);
    }

    @GetMapping("/getTotal")
    public Result<List<KeyAll>> getTotal() {
        return Result.success(keyWordService.getAll());
    }

}
